﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using MvcShop.Models;

namespace MvcShop.MVC.HtmlHelpers
{
    public static class HtmlHelperExtensions
    {
        public static string ThumbnailImg(this HtmlHelper helper,string imgPath)
        {
            string[] tImgPath=imgPath.Split('.');
            return tImgPath[0] + "-thumnail.png";
        }

        public static string ThumbnailImg(this HtmlHelper helper,string imgPath,string productName)
        {
            if (!String.IsNullOrEmpty(imgPath))
            {
                string[] tImgPath = imgPath.Split('.');
                string path = tImgPath[0] + "-thumnail.png";
                return "<img src=\"/{0}\" alt=\"{1}\" title=\"{1}\" border=\"0\" />".With(helper.Encode(path), helper.Encode(productName));
            }
            else
            {
                return "<img src=\"/uploads/def_exp010.gif\" alt=\"{0}\" title=\"{0}\" border=\"0\" />".With(helper.Encode(productName));
            }
        }

        public static string OrdersStatus(this HtmlHelper helper,bool? isInvalid, bool? isActive, bool? isSend, bool? isPayment)
        {
            bool _isInvalid = isInvalid ?? false;
            bool _isActive = isActive ?? false;
            bool _isSend = isSend ?? false;
            bool _isPayment = isPayment ?? false;
            string query;

            if (!_isInvalid)
            {
                if (_isPayment)
                {
                    query = "已支付";
                }
                else
                {
                    query = "未支付";
                }

                if (_isActive)
                {
                    if (_isSend)
                    {
                        query += " 已发货";
                    }
                    else
                    {
                        query += " 未发货";
                    }
                }
                query += " 未审核";

            }
            else 
            {
                query = "已作废";
            }
            return query;
        }

        public static string GetDifference(this HtmlHelper helper, decimal? max, decimal? mim)
        {
            return String.Format("{0:F}", max - mim);
        }

        public static string GetDiscount(this HtmlHelper helper, decimal? max, decimal? mim)
        {
            int dis = (int)((mim / max) * 100);
            return dis.ToString();
        }

        public static string WriteOnlinePoll(this HtmlHelper helper, IEnumerable<PollOption> pollOptions, string selectmode)
        {
            var pollWriter = new OnlinePollWriter(helper, pollOptions, selectmode);
            return pollWriter.Write();
        }
    }
}
